
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>缓存机制 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>缓存机制 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">MyBatis支持哪两种级别的缓存？它们的作用范围有何不同？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">MyBatis支持两级缓存。一级缓存是`SqlSession`级别的缓存，其生命周期仅限于一个`SqlSession`实例。二级缓存是`SqlSessionFactory`级别的缓存，可以跨`SqlSession`共享。</div>
          </div>
          <div class="card-source">来源: 1. 缓存的基本结构与缓存类型</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">MyBatis一级缓存的生命周期是怎样的？它在何时会被清除？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">一级缓存的生命周期与`SqlSession`的生命周期一致。当`SqlSession`关闭时，一级缓存会被清除。此外，在提交事务时，MyBatis也会调用`clearCache()`方法清空一级缓存。</div>
          </div>
          <div class="card-source">来源: 2. 一级缓存的实现与生命周期, 6. 缓存清理与失效策略</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">MyBatis中一级缓存的核心实现类是什么？它内部使用什么数据结构来存储缓存数据？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">MyBatis一级缓存的核心实现类是`LocalCache`。在`LocalCache`内部，它使用一个`HashMap`来存储缓存数据。</div>
          </div>
          <div class="card-source">来源: 3. LocalCache的实现</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">如何在MyBatis的Mapper XML文件中启用二级缓存？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">在Mapper XML文件的`<mapper>`标签内部，通过添加空的`<cache />`标签来启用当前映射器的二级缓存。</div>
          </div>
          <div class="card-source">来源: 4. 二级缓存的实现与配置</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">MyBatis二级缓存的默认实现类是什么？它的数据存储方式是怎样的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">MyBatis二级缓存的默认实现类是`PerpetualCache`。`PerpetualCache`是`Cache`接口的标准实现，它通过一个`HashMap`来存储缓存数据。</div>
          </div>
          <div class="card-source">来源: 4. 二级缓存的实现与配置</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">当同时启用一、二级缓存时，MyBatis的查询缓存策略（查找顺序）是怎样的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">MyBatis会先检查一级缓存（`LocalCache`）。如果一级缓存没有命中，它会再去二级缓存中查找。如果两级缓存都没有命中，MyBatis才会执行数据库查询，并将结果存入缓存。</div>
          </div>
          <div class="card-source">来源: 7. 缓存的查询策略</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">在什么情况下MyBatis会自动清理或使缓存失效？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">当执行数据更改操作（如`INSERT`、`UPDATE`、`DELETE`）时，缓存内容可能变得无效，MyBatis会清除相关的缓存。此外，在提交事务时，MyBatis也会调用`clearCache()`方法来清空一级和二级缓存。</div>
          </div>
          <div class="card-source">来源: 6. 缓存清理与失效策略</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">MyBatis中用于定义缓存基本操作的核心接口是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">MyBatis中定义缓存基本操作的核心接口是`Cache`接口。它定义了如`putObject`、`getObject`、`removeObject`、`clear`等方法。</div>
          </div>
          <div class="card-source">来源: 1. 缓存的基本结构与缓存类型</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">一级缓存和二级缓存的主要区别是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">主要区别在于作用范围和生命周期。一级缓存是`SqlSession`级别的，默认开启，生命周期与`SqlSession`绑定，无法跨`SqlSession`共享。二级缓存是`SqlSessionFactory`级别的（也称为mapper级别），可以跨`SqlSession`共享，通常用于跨请求的缓存。</div>
          </div>
          <div class="card-source">来源: 1. 缓存的基本结构与缓存类型</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
